/***斗地主带机器人房间数据类
 *  
 */
var DdzhRoom = require('./DdzhRoom');
var DdzhSeatRobot = require('./DdzhSeatRobot');
var DdzhSeat = require('./DdzhSeat');
var _ = require('./../../FrameWork/we_underscore');
var EnumRole = require('./../../PkCommon/Enum/EnumRole');
var EnumPkRoom = require('./../../PkCommon/Enum/EnumPkRoom');
var PkCardTool = require('./../../PkCommon/FindTool/PkCardTool');
var ClientEveDelegate = require('../../FrameWork/Delegate/ClientEveDelegate');
var ClientPkDelegateType = require('../../PkCommon/Delegate/ClientPkDelegateType');
var MgTimeOut = require('../../FrameWork/Manager/MgTimeOut');
module.exports = cc.Class({
    extends: DdzhRoom,
    ctor() {
        this.joinSeatSelf();
        this.joinSeatRobot();
        this.joinSeatRobot();
    },

    /**智能操作机器人是否要牌
     * return false直接不要，true检查是否要得起如果要得起就直接压
     */
    autoCheckIsGet() {
        //不压朋友的牌        
        var isFriend = this.getLastOutIsFriend(this.currSeatIdx);
        if (isFriend) {
            return false;
        }
        return true;
    },
    /**
     * 获取_seatIdx上家座位号
     * _seatIdx:座位id
     */
    getBeforeSeatIdx(_seatIdx) {
        let _max = this.getMaxSeatCnt();
        return _seatIdx >= 1 ? (_seatIdx - 1) : _max;
    },
    /**
     * 获取_seatIdx下家座位号
     * _seatIdx:座位id
     */
    getAfterSeatIdx(_seatIdx) {
        let _max = this.getMaxSeatCnt();
        return _seatIdx < (_max - 1) ? (_seatIdx + 1) : 0;
    },

    /**上一个出牌的是否为朋友 */
    getLastOutIsFriend(_seatIdx) {
        /**最后出牌的是否为庄家 */
        var lastOutIsLandlord = this.getIsLandlord(this.lastOutCardInfo.seatIdx);
        var _seatIdxIsLandlord = this.getIsLandlord(_seatIdx);
        return lastOutIsLandlord == _seatIdxIsLandlord;
    },
    joinSeatSelf() {
        let selfSeat = new DdzhSeat();
        selfSeat.setSeatIdx(_.size(this.seatObjList));
        this.joinSeat(selfSeat);
    },

    /***加入一个 */
    joinSeatRobot() {
        let robot = new DdzhSeatRobot();
        robot.setSeatIdx(_.size(this.seatObjList));
        this.joinSeat(robot);
    },

    getMoney(_seatIdx) {
        if (!!this.seatObjList && !!this.seatObjList[_seatIdx]) {
            return this.seatObjList[_seatIdx].getMoney();
        }
        return 0;
    },
    /***进入房间就开始发牌 */
    serverFirstSendCard() {
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.TestRobotHandCardsRmAll);
        var seatList = [];
        /**生成一幅牌 */
        var allCards = [];
        for (var cardId = 1; cardId <= 54; ++cardId) {
            allCards.push(cardId);
        }
        var maxSeatCnt = this.getMaxSeatCnt();
        for (var i = 0; i < maxSeatCnt; ++i) {
            /**************************************************/
            var randCards = [];
            //随机13个牌
            for (var randNum = 1; randNum <= 17; ++randNum) {
                var maxNum = _.size(allCards);
                var idx = _.random(0, maxNum - 1);
                var randCardId = allCards[idx];
                randCards.push(randCardId);
                allCards = _.difference(allCards, [randCardId]);
            }
            var cardLeftCnt = _.size(randCards);

            // 随机出来的牌：[15,21,24,7,51,36,42,49,1,20,3,37,47,46,11,34,26]
            // DdzhRoomRobot.js:66
            // 随机出来的牌：[41,39,40,5,31,29,9,52,4,35,53,27,48,30,17,16,33]
            // DdzhRoomRobot.js:66
            // 随机出来的牌：[14,12,28,45,2,25,23,43,10,44,6,19,38,18,32,50,13]
            // randCards = [9, 10, 14, 19, 21, 28, 34, 35, 36, 40, 41, 32, 45, 48, 49, 53, 54] //飞机
            // randCards = [2, 4, 11, 12, 17, 21, 23, 25, 26, 29, 34, 36, 38, 40, 49, 50, 51]; //连对
            // randCards = [54, 2, 3, 14, 15, 16, 27, 28, 29, 40, 41, 42, 38, 40, 49, 50, 51]; //多个炸
            // if (0 == i) {

            //     randCards = _.sortBy(randCards)
            //     cc.error('随机出来的牌：' + JSON.stringify(randCards));
            //     cc.error('随机出来的牌：' + JSON.stringify(PkCardTool.getCardInfos(randCards)));

            // }

            // var TmpCards = [
            //     [5, 25, 14, 27, 40, 13, 21, 2, 38, 19, 4, 28, 43, 34, 33, 51, 50],

            //     [30, 37, 52, 44, 31, 24, 3, 48, 8, 23, 12, 20, 45, 1, 36, 16, 22],

            //     [49, 18, 26, 6, 42, 11, 9, 46, 41, 39, 7, 15, 32, 35, 10, 17, 54]
            // ] 
            cc.error(JSON.stringify(randCards));
            seatList.push({
                playerId: i,
                playName: "tony" + i,
                money: this.getMoney(i), //_.random(100, 1000),
                seatIdx: i,
                cardIds: randCards, //randCards,TmpCards[i],
                cardLeftCnt: cardLeftCnt
            });

            if (i > 0) {
                ClientEveDelegate.doDataToLister(ClientPkDelegateType.TestRobotAddHandCards, seatList[i]);
            }
        }
        cc.error('底牌：' + JSON.stringify(allCards));
        var roomInfo = {
            bankerSeatIdx: 0,
            roomState: EnumPkRoom.CALLSCORING,
            fristOutSeatIdx: -1, //第一个出牌的玩家（抢完庄之后设定）
            lastOutCardInfo: null,
            callScoreInfo: {},
            doubleInfo: {},
            bottomCardInfo: {
                seatIdx: 0,
                cardIds: allCards
            },
            seatList: seatList
        }
        this.sendFristCard(roomInfo);
    },
    /***出牌动作
     * _outCardInfo:{seatIdx:1,type:1,cardIds:[]}
     */
    outCardAct(_outCardInfo) {
        var cardLeftCnt = this._super(_outCardInfo);
        if (0 == cardLeftCnt) {
            this.refreshRoomState(EnumPkRoom.GAMINGEND);
        }
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.TestRobotRmHandCards, _outCardInfo);

        return cardLeftCnt;
    },
    /***要不起 
     * info:{
     *   seatIdx:1
     * }
     */
    noOut(info) {
        var nextOutCardSeatIdx = this._super(info);
        return nextOutCardSeatIdx;
    },
    /***设置为要操作的座位*/
    setCurrOutSeatIdx(_seatIdx) {
        this._super(_seatIdx);
        if (!!_seatIdx) {
            var roleType = this.seatObjList[_seatIdx].getRoleType();
            /***轮到机器人时启动机器人AI */
            if (EnumRole.ROBOT == roleType) {
                if (null != this.lastOutCardInfo) {
                    if (this.lastOutCardInfo.seatIdx == _seatIdx) {
                        //没人要的起可以重新出任意牌型 
                        /***轮到机器人时启动机器人AI */
                        this.seatObjList[_seatIdx].doAIRandOutCard();
                        return;
                    }
                }
                this.seatObjList[_seatIdx].doAI(this.lastOutCardInfo);
            }
        }
    },

    /***刷新叫分
     * _callScoreInfo:{
     * seatIdx:1,
     * score:1
     * }
     * return -1//返回下一个操作的玩家
     */
    refreshCallScore(_callScoreInfo) {
        var nextSeatIdx = this._super(_callScoreInfo);
        if (nextSeatIdx >= 0) {
            var roleType = this.seatObjList[nextSeatIdx].getRoleType();
            /***轮到机器人时启动机器人AI */
            if (EnumRole.ROBOT == roleType) {
                this.seatObjList[nextSeatIdx].doAICallScore();
            }
        }
    },

    /***刷新房间状态 */
    refreshRoomState(_roomState) {
        this._super(_roomState);
        /***开始加倍 */
        if (EnumPkRoom.DOUBLING == _roomState) {
            this.fristOutSeatIdx = this.landlordSeatIdx; //第一个出牌的玩家  
            _.each(this.seatObjList, function(_seat) {
                var roleType = _seat.getRoleType();
                /***轮到机器人时启动机器人AI */
                if (EnumRole.ROBOT == roleType) {
                    _seat.doAIDuble();
                }
            }.bind(this));
        } else if (EnumPkRoom.WAITFRITING == _roomState) {
            if (this.fristOutSeatIdx != this.selfSeatIdx) {
                var _seat = this.seatObjList[this.fristOutSeatIdx];
                var roleType = _seat.getRoleType(); /***轮到机器人时启动机器人AI */
                if (EnumRole.ROBOT == roleType) {
                    _seat.doAIRandOutCard();
                }
            }
        } else if (EnumPkRoom.SENDBOTTOMCARDS == _roomState) {
            // this.bottomCardInfo

            ClientEveDelegate.doDataToLister(ClientPkDelegateType.TestRobotAddHandCards, this.bottomCardInfo);
        } else if (EnumPkRoom.GAMINGEND == _roomState) {
            //结算的时候延时1.5秒出现结算界面
            MgTimeOut.do(function() {
                let endInfo = this.getGameEndInfo();
                this.doGameEnd(endInfo);
            }.bind(this), 1000);
        }
    },

    getGameEndInfo() {
        let lastSeatIdx = this.lastOutCardInfo.seatIdx;

        /**是否为地主赢*/
        let isLandlordWin = this.isLandlordSeatIdx(lastSeatIdx);

        let endInfo = {
            seatInfos: []
        }
        this.each(function(seatObj) {
            let _seatIdx = seatObj.getSeatIdx();
            let isLandlord = this.isLandlordSeatIdx(_seatIdx);
            let money = seatObj.getMoney();
            let tmp = !!isLandlordWin ? 1 : -1;
            let currChange = (!!isLandlord ? 100 * tmp : -50 * tmp);
            let resultMoney = money + currChange;
            seatObj.setMoney(resultMoney);
            endInfo.seatInfos.push({
                seatIdx: _seatIdx,
                startNumber: 0,
                endNumber: currChange
            });
        }.bind(this));
        return endInfo;
    }
});